
/*
现在只支持一组等长网络处理.
    (可以删除已经画好的网络,保留正在画的网络)

    1.需要选择  "网络模式.如a01.png"
    2.按F12调出调试模式,如a02.png
    3.在'console'窗口下运行当前源码 (复制粘贴当前源码)
        会生成parseLen 函数
    4.选择指定的线段,shift-A, tab 切换指定长度.如a03.png
    5.又切换回'console'运行 parseLen()函数 .如a04.png
        parseLen();
*/

function findElement(tag,name,eq){
    //查看
    var spans = document.getElementsByTagName(tag);
    for (let index = 0; index < spans.length; index++) {
        const span = spans[index];
        if(span.innerText!==undefined){
            if(eq){
                if(span.innerText==name)
                    return span;
            }
            else if(span.innerText.indexOf(name)>=0){
                return span;
            }
        }
    }
    return null;
}

function findSubNetGrop(name){
    return findElement('span',name);
}

function findSubNetId(){
    var a = findElement('div','网络长度',true);
    if(a){
        var x =
            a.parentElement
            //document
            .getElementsByTagName('input');
        for (let index = 0; index < x.length; index++) {
            const input = x[index];
            //遍历所有输入
            var parent = input;
            while(parent && parent.innerText=='') parent =parent.parentElement; //直到innerText不为空
            if(parent.innerText=='网络'){
                return input.value;
            }
        }
        return null;
    }
}

function findNetGrop(name){
    var sub_span= findSubNetGrop(name);
    if(sub_span){
        // //获得classname
        // var class_name = sub_span.className;

        //查找父类,直到出现等长网络组
        var parent = sub_span;
        while(parent){
            parent = parent.parentElement;
            if(parent.innerText.indexOf('等长网络组')>0){
                xtxt = parent.innerText;
                console.log(parent.innerText);
                var array = [];
                var str = parent.innerText;
                var regexp = /\n([$\w]+)\s+\(([\d.]+)\w+\)/g;
                var match;
                while((match=regexp.exec(str))){
                    var len= parseFloat(match[2]);
                    var name=match[1];
                    array.push({name:name,len:len});
                }
                return array;
            }
        }
    }
    return [];
}

function findEditLenInput(){
    var a = findElement('div','要增加的长度:',true);
    if(a)
        return a.getElementsByTagName('input')[0];
    return null;
}

function parseRRR(name){
    var list = findNetGrop(name);

    //保留表
    netTable = {};
    netTableLen = {};

    if(window.update_dnterval_id){
        clearInterval(window.update_dnterval_id);
    }

    maxLen = 0;
    window.edit_count=0;
    parseLen = function(){
        let r = findEditLenInput();
        if(!r){
            window.initd = false;
            window.edit_count  = 0;
            return;
        }


        if(window.edit_count>1){
            a = findElement('div','要增加的长度:',true);
            a.getElementsByTagName('span')[0].innerHTML="完成修改!"
            return;
        }
        //let rtitle = document.getElementsByName('attr-select-net')[0].title;
        let select = findSubNetId();        //选择的网络名

        let len = netTableLen[select];
        r.focus();
        //r.eit();
        r.value = /* maxLen.toString() + '-' + len.toString(); */
            maxLen - len;
        txt = `${select},${maxLen},${len}`;
        window.edit_count ++;
        return maxLen-len;
    }

    list.sort((a,b)=>{
        let alen = (a.len);
        let blen = (b.len);
        if(maxLen<alen) maxLen = alen;
        if(maxLen<blen) maxLen = blen;
        return alen>blen;
    })

    list.forEach(element => {
        netTable[element.name] = element;
        netTableLen[element.name] = element.len;
    });

    window.update_dnterval_id = setInterval(parseLen,1000);
}

parseRRR('1N118');